﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using AEM.Data;

namespace AEM.Controls
{
    /// <summary>
    /// FireModel.xaml 的交互逻辑
    /// </summary>
    public partial class FireModel : UserControl
    {
        public FireModel()
        {
            InitializeComponent();
        }

        public static void OnLiftNumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                int num = (int)e.NewValue;
                if (null != Config.LiftInftList)
                {
                    Data.Model.LiftInfo row = (from Data.Model.LiftInfo r in Config.LiftInftList
                                               where r.LiftNum == num
                                               select r).FirstOrDefault();

                    if (null != row)
                    {
                        FireModel fm = (sender as FireModel);
                        fm.lift_name.Content = row.LiftSite;
                        fm.type_lift.Visibility = Visibility.Hidden;
                        fm.type_e.Visibility = Visibility.Hidden;
                        fm.type_t.Visibility = Visibility.Hidden;

                        switch ((Data.Enum.LiftType)row.LiftType)
                        {
                            case Data.Enum.LiftType.电梯:
                                fm.type_lift.Visibility = Visibility.Visible;
                                break;
                            case Data.Enum.LiftType.扶梯:
                                fm.type_e.Visibility = Visibility.Visible;
                                break;
                            case Data.Enum.LiftType.人行步道:
                                fm.type_t.Visibility = Visibility.Visible;
                                break;
                        }
                    }
                }
            }
        }

        public static void OnFloorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
                (sender as FireModel).lift_floor.Content = (string)e.NewValue;
        }

        private static void OnLiftRisingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if ((bool)args.NewValue)
                {
                    (sender as FireModel).up_ring_status.Fill = new SolidColorBrush(Color.FromRgb(0xEF, 0xEF, 0xEF));
                    (sender as FireModel).e_up.Visibility = Visibility.Visible;
                }
                else
                {
                    (sender as FireModel).up_ring_status.Fill = new SolidColorBrush(Color.FromRgb(0x4D, 0x4D, 0x4D));
                    (sender as FireModel).e_up.Visibility = Visibility.Hidden;
                }
            }
        }

        private static void OnLiftDescendingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if ((bool)args.NewValue)
                {
                    (sender as FireModel).down_ring_status.Fill = new SolidColorBrush(Color.FromRgb(0xEF, 0xEF, 0xEF));
                    (sender as FireModel).e_down.Visibility = Visibility.Visible;
                }
                else
                {
                    (sender as FireModel).down_ring_status.Fill = new SolidColorBrush(Color.FromRgb(0x4D, 0x4D, 0x4D));
                    (sender as FireModel).e_down.Visibility = Visibility.Hidden;
                }
            }
        }

        private static void OnDoorStatusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                Storyboard s = (Storyboard)(sender as FireModel).FindResource("DoorCloseStoryboard");
                if ((bool)args.NewValue)
                {
                    s = (Storyboard)(sender as FireModel).FindResource("DoorOpenStoryboard");
                }
                (sender as FireModel).BeginStoryboard(s);
            }
        }

        #region 注册属性
        public static readonly DependencyProperty LiftNumProperty =
            DependencyProperty.Register("LiftNum", typeof(int), typeof(FireModel),
            new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnLiftNumChanged)));

        public int LiftNum
        {
            get { return (int)GetValue(LiftNumProperty); }
            set { SetValue(LiftNumProperty, value); }
        }

        public static readonly DependencyProperty FloorProperty =
            DependencyProperty.Register("Floor", typeof(string), typeof(FireModel),
            new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnFloorChanged)));

        public string Floor
        {
            get { return (string)GetValue(FloorProperty); }
            set { SetValue(FloorProperty, value); }
        }

        /// <summary>
        /// 电梯运行方向,上行true有效 false无效
        /// </summary>
        public static readonly DependencyProperty LiftRisingProperty =
            DependencyProperty.Register("LiftRising", typeof(bool), typeof(FireModel),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnLiftRisingChanged)));

        /// <summary>
        /// 电梯运行方向,上行true有效 false无效
        /// </summary>
        public bool LiftRising
        {
            get { return (bool)GetValue(LiftRisingProperty); }
            set { SetValue(LiftRisingProperty, value); }
        }

        /// <summary>
        /// 电梯运行方向,下行true有效 false无效
        /// </summary>
        public static readonly DependencyProperty LiftDescendingProperty =
            DependencyProperty.Register("LiftDescending", typeof(bool), typeof(FireModel),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnLiftDescendingChanged)));

        /// <summary>
        /// 电梯运行方向,下行true有效 false无效
        /// </summary>
        public bool LiftDescending
        {
            get { return (bool)GetValue(LiftDescendingProperty); }
            set { SetValue(LiftDescendingProperty, value); }
        }

        /// <summary>
        /// 电梯门状态,true开启,false关闭
        /// </summary>
        public static readonly DependencyProperty DoorStatusProperty =
            DependencyProperty.Register("DoorStatus", typeof(bool), typeof(FireModel),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnDoorStatusChanged)));

        /// <summary>
        /// 电梯门状态,true开启,false关闭
        /// </summary>
        public bool DoorStatus
        {
            get { return (bool)GetValue(DoorStatusProperty); }
            set { SetValue(DoorStatusProperty, value); }
        }
        #endregion
    }
}
